import { App } from "vue";
import FormRender from "./common/FormRender";

function getComponentName(key: string) {
  if (!key) {
    return "";
  }
  const paths = key.split("/");
  const name = paths
    .filter((it) => !!it && it !== ".")
    .reverse()
    .find((it) => it !== "index.vue" && it !== "index.ts" && it !== "index.js")
    ?.replace(".vue", "");
  return name || "";
}

export async function registerComponents(app: App) {
  const components = import.meta.glob("./**/*.vue");
  // 等待所有组件加载完成
  const promises = Object.keys(components).map(async (it) => {
    const component = await components[it]();
    const componentName = component.default.name || getComponentName(it);
    app.component(componentName, component.default);
  });
  await Promise.all(promises);
}
